package com.yugao.fintech.draper.security.service.impl;

import com.yugao.fintech.draper.auth.api.AuthFeignApi;
import com.yugao.fintech.draper.auth.api.UserFeignApi;
import com.yugao.fintech.draper.auth.dto.UserInfoVO;
import com.yugao.fintech.draper.core.constant.SecurityConstants;
import com.yugao.fintech.draper.core.exception.BaseException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.yugao.fintech.draper.core.util.ResultConsumer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class SecurityUserManager {

    private final UserFeignApi userFeignApi;

    private final AuthFeignApi authFeignApi;

    public UserInfoVO findByName(String username) {
        // @formatter:off
		return ResultConsumer.ofNullable(userFeignApi.findByName(username, SecurityConstants.FROM_IN))
				.assertSuccess(r -> new BaseException(String.format("根据用户名查询用户失败:%s,%s", username, r.getMsg())))
				.peek().getData();
		// @formatter:on
    }

    public UserInfoVO findByMobile(String mobile) {
        // @formatter:off
		return ResultConsumer.ofNullable(userFeignApi.findByMobile(mobile, SecurityConstants.FROM_IN))
				.assertSuccess(r -> new BaseException(String.format("根据手机号查询用户失败:%s,%s", mobile, r.getMsg())))
				.peek().getData();
		// @formatter:on
    }

    public String findUserPermissions(String username) {
        // @formatter:off
		Set<String> permissions = ResultConsumer.ofNullable(authFeignApi.findUserPermissions(username, SecurityConstants.FROM_IN))
				.assertSuccess(r -> new BaseException(String.format("查询用户权限失败:%s,%s", username, r.getMsg())))
				.peek().getData();
		// @formatter:on
        return String.join(StringPool.COMMA, permissions);
    }
}
